<template>
    <div class="page-box">
        <Card>
            <Button size="small" type="primary" @click="returnDaily">返回</Button>
            <el-table
                :data="data"
                height="calc(100vh - 195px)"
                border
                class="margin-top-5"
                style="width: 100%"
                size="mini"
                highlight-current-row
                :header-cell-style="headerCellStyle"
                oncontextmenu="return false;"
                ref="multipleTable"
                sortable="custom"
                empty-text="暂无数据"
                v-loading="dataLoading"
                show-summary
                :summary-method="getSummaries"
                @sort-change="onTableSort">
                <el-table-column
                    type="index"
                    align="center"
                    label="序号"
                    width="55">
                </el-table-column>
                <el-table-column
                    v-for="(item, key) in columns" :key="key"
                    :prop="item.key"
                    :label="item.title"
                    :width="item.width"
                    align="center"
                    :formatter="item.formatter"
                    :sortable="item.sortable"
                    :show-overflow-tooltip="true"
                    :fixed="item.key=='userName'"
                >
                    <template slot-scope="scope">
                        <template v-if="item.type !== 'template'">
                            {{$table.tableRowFormat(scope.row, item)}}
                        </template>
                    </template>
                </el-table-column>
            </el-table>
            <Row class="margin-top-10">
                <Page :total="total" :current="listQuery.pageNum" :page-size="listQuery.pageSize"
                      :page-size-opts="listQuery.pageOptionsList"
                      size="small" @on-change="pageChange" @on-page-size-change="pageSizeChange" show-elevator
                      show-sizer></Page>
            </Row>
        </Card>
    </div>
</template>

<script>
import { mapState } from 'vuex';
import {
    inStockTraceCodeList,
    inStockTraceCodeListSum,
    outStockTraceCodeList,
    outStockTraceCodeListSum
} from "@/api/warehouse/home.js";

export default {
    name: "TraceInfo",
    data() {
        return {
            totalData: {},
            sumData: {},
            data: [],
            inColumns: [
                {
                    key: 'endInTime',
                    title: '入库时间',
                    width: 150,
                    type: 'datetime'
                },
                {
                    key: 'ownerName',
                    title: '货主',
                    width: 170
                },
                {
                    key: 'manageName',
                    title: '运营公司',
                    width: 180
                },
                {
                    key: 'whName',
                    title: '仓库',
                    width: 150
                },
                {
                    key: 'businessNum',
                    title: '箱号',
                    width: 120
                },
                {
                    key: 'businessType',
                    title: '业务类型',
                    width: 120
                },
                {
                    key: 'matType',
                    title: '品类',
                    width: 120
                },
                {
                    key: 'matName',
                    title: '品名',
                    width: 120
                },
                {
                    key: 'traceCode',
                    title: '托盘号',
                    width: 120
                },
                {
                    key: 'realQty',
                    title: '件数/件',
                    width: 120
                },
                {
                    key: 'realWeight',
                    title: '重量/KG',
                    width: 120
                }
            ],
            outColumns: [
                {
                    key: 'endInTime',
                    title: '出库时间',
                    width: 150,
                    type: 'datetime'
                },
                {
                    key: 'ownerName',
                    title: '货主',
                    width: 170
                },
                {
                    key: 'manageName',
                    title: '运营公司',
                    width: 180
                },
                {
                    key: 'whName',
                    title: '仓库',
                    width: 150
                },
                {
                    key: 'businessNum',
                    title: '箱号',
                    width: 120
                },
                {
                    key: 'carNo',
                    title: '车号',
                    width: 120,
                },
                {
                    key: 'businessType',
                    title: '业务类型',
                    width: 120
                },
                {
                    key: 'matType',
                    title: '品类',
                    width: 120
                },
                {
                    key: 'matName',
                    title: '品名',
                    width: 120
                },
                {
                    key: 'traceCode',
                    title: '托盘号',
                    width: 120
                },
                {
                    key: 'realQty',
                    title: '件数/件',
                    width: 120
                },
                {
                    key: 'realWeight',
                    title: '重量/KG',
                    width: 120
                }
            ],
            dataLoading: false,
            total: 0,
            listQuery: {
                pageNum: this.$store.state.sysSettings.pageNum,
                pageSize: this.$store.state.sysSettings.pageSize,
                pageOptionsList: this.$store.state.sysSettings.pageOptionsList,
                filterList: [],
                sortList: [],
                searchKey: ''
            },
        }
    },
    computed: {
        ...mapState('task', {
            headerCellStyle: state => state.headerCellStyle
        })
    },
    methods: {
        returnDaily() {
            this.$router.back()
        },
        initGridData() {
            if (this.$route.query) {
                // 接收传参
                let item = this.$route.query;
                if (this.$route.query) {
                    // 接收传参
                    let item = this.$route.query;
                    this.listQuery.filterList.push({
                        filterKey: 'whCode',
                        filterValue: item.whCode
                    });
                    this.listQuery.filterList.push({
                        filterKey: 'time',
                        filterValue: item.time
                    });
                    this.listQuery.filterList.push({
                        filterKey: 'ownerCode',
                        filterValue: item.ownerCode
                    });
                    this.listQuery.filterList.push({
                        filterKey: 'manageCompanyCode',
                        filterValue: item.manageCompanyCode
                    });
                    this.listQuery.filterList.push({
                        filterKey: 'businessNum',
                        filterValue: item.businessNum
                    });
                    this.listQuery.filterList.push({
                        filterKey: 'carNo',
                        filterValue: item.carNo
                    });
                    this.listQuery.filterList.push({
                        filterKey: 'outPlanNum',
                        filterValue: item.outPlanNum
                    });
                    this.dataLoading = true;
                    if (item.type == 'in') {
                        inStockTraceCodeList(this.listQuery, this.$route.query.wmsUrl).then(response => {
                            this.dataLoading = false;
                            if (response.data.returnCode === 200) {
                                this.data = response.data.returnData.pageInfo.list;
                                this.total = response.data.returnData.pageInfo.total;
                                this.$nextTick(() => {
                                    this.$refs.multipleTable.doLayout();
                                });
                            } else {
                                // this.$error(response.data.returnMsg, 3);
                            }
                        }).catch(err => {
                            console.log(err);
                            this.dataLoading = false;
                            this.$loginOut();
                        });
                        this.getSumInfo(item);
                    } else {
                        outStockTraceCodeList(this.listQuery, this.$route.query.wmsUrl).then(response => {
                            this.dataLoading = false;
                            if (response.data.returnCode === 200) {
                                this.data = response.data.returnData.pageInfo.list;
                                this.total = response.data.returnData.pageInfo.total;
                                this.$nextTick(() => {
                                    this.$refs.multipleTable.doLayout();
                                });
                            } else {
                                // this.$error(response.data.returnMsg, 3);
                            }
                        }).catch(err => {
                            console.log(err);
                            this.dataLoading = false;
                            this.$loginOut();
                        });
                        this.getSumInfo(item);
                    }
                }
                // this.dataLoading = true;
            }
        },
        getSumInfo(item) {
            if (item.type == 'in') {
                inStockTraceCodeListSum(this.listQuery, this.$route.query.wmsUrl).then(response => {
                    if (response.data.returnCode === 200) {
                        this.totalData = response.data.returnData.map;
                    } else {
                        this.$error(response.data.returnMsg, 3);
                    }
                }).catch(err => {
                    console.log(err);
                });
            } else {
                outStockTraceCodeListSum(this.listQuery, this.$route.query.wmsUrl).then(response => {
                    if (response.data.returnCode === 200) {
                        this.totalData = response.data.returnData.map;
                    } else {
                        this.$error(response.data.returnMsg, 3);
                    }
                }).catch(err => {
                    console.log(err);
                });
            }
        },
        selectionChange(selection) {
            this.selectDataList = selection
        },
        getSummaries(param) {
            const {columns, data} = param;
            const sums = [];
            columns.forEach((column, index) => {
                if (index === 0) {
                    sums[index] = '合';
                    return;
                }
                if (this.$route.query) {
                    let type = this.$route.query.type;
                    if (type === 'in'){
                        if (index === 9 || index === 10 || index === 11) {
                            const values = data.map(item => Number(item[column.property]));
                            if (!values.every(value => isNaN(value))) {
                                switch (index) {
                                    case 11:
                                        sums[index] = this.totalData.sumRealWeight + 'KG';
                                        break;
                                    case 10:
                                        sums[index] = this.totalData.sumRealQty + '件';
                                        break;
                                    case 9:
                                        sums[index] = this.totalData.sumTrace + '托';
                                        break;
                                }
                            } else {
                                sums[index] = '';
                            }
                        }
                    }else {
                        if (index === 10 || index === 11 || index === 12) {
                            const values = data.map(item => Number(item[column.property]));
                            if (!values.every(value => isNaN(value))) {
                                switch (index) {
                                    case 12:
                                        sums[index] = this.totalData.sumRealWeight + 'KG';
                                        break;
                                    case 11:
                                        sums[index] = this.totalData.sumRealQty + '件';
                                        break;
                                    case 10:
                                        sums[index] = this.totalData.sumTrace + '托';
                                        break;
                                }
                            } else {
                                sums[index] = '';
                            }
                        }
                    }
                }
            });
            return sums;
        },
        // 排序
        onTableSort(result) {
            if (result.order === 'descending') {
                let sortItem = {
                    sortKey: result.prop,
                    sortValue: 'desc'
                };
                this.listQuery.sortList = [sortItem]
            } else if (result.order === 'ascending') {
                let sortItem = {
                    sortKey: result.prop,
                    sortValue: 'asc'
                };
                this.listQuery.sortList = [sortItem]
            } else {
                this.listQuery.sortList = []
            }
            this.initGridData();
        },
        // 改变页数,
        pageChange(pageNum) {
            this.listQuery.pageNum = pageNum;
            this.initGridData();
        },
        // 改变条数
        pageSizeChange(pageSize) {
            this.listQuery.pageSize = pageSize;
            this.initGridData();
        }
    },
    mounted() {
        if (this.$route.query) {
            let type = this.$route.query.type;
            this.columns = type === 'in' ? this.inColumns : this.outColumns;
        }
        this.initGridData();
    }
}
</script>

<style scoped lang="less">
@import './common.less';
</style>
